unit JATUIF_Component_Editbox;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, JATUIF_Component, JATUIF_Driver_Screen;

Type

    { TEditBox }

    TEditBox = class(TComponent)
      Private
        { Internal Values }
        Edit_X : Integer;
        Edit_Y : Integer;
        Edit_Width : Integer;
        Edit_Text : String;
        Edit_Color : Byte;
        Edit_Background : Byte;
        Edit_Masked : Boolean;
        Edit_MaskChar : Char;
        Edit_MaxChars : Integer;
        { Property Methods }
        Procedure SetText(Text:String);
        Function GetText:String;
        Procedure SetTextColor(Col:Byte);
        Function GetTextColor:Byte;
        Procedure SetBackgroundColor(Col:Byte);
        Function GetBackgroundColor:Byte;
        Procedure SetMasked(Masked:Boolean);
        Function GetMasked:Boolean;
        Procedure SetMaskChar(MChar:Char);
        Function GetMaskChar:Char;
        Procedure SetMaxChars(Mx:Integer);
        Function GetMaxChars:Integer;
      Protected
        Procedure _OnKeyPress(Key:Char); override;
        Procedure _OnMouseEnter; override;
        Procedure _OnMouseLeave; override;
        Procedure _OnClick; override;
        Procedure Draw; override;
      Public
        { Constructor }
        Constructor Create(Parent: TScreen; X,Y:Integer; Width:Integer; TextColor:Byte; EditColor:Byte; Masked:Boolean);
        { Properties }
        Property Text : String read GetText write SetText;
        Property Text_Color : Byte read GetTextColor write SetTextColor;
        Property Background_Color : Byte read GetBackgroundColor write SetBackgroundColor;
        Property Masked : Boolean read GetMasked write SetMasked;
        Property Mask_Char : Char read GetMaskChar write SetMaskChar;
        Property Max_Chars : Integer read GetMaxChars write SetMaxChars;
    end;

implementation

{ TEditBox }

constructor TEditBox.Create(Parent: TScreen; X, Y: Integer; Width: Integer; TextColor: Byte; EditColor: Byte; Masked: Boolean);
Var
   X1,Y1,X2,Y2:Integer;

begin
     Edit_X:=X;
     Edit_Y:=Y;
     Edit_Width:= Width;
     X1:=X;
     Y1:=Y;
     X2:=X1+Width;
     Y2:=Y;
     Edit_Color:= TextColor;
     Edit_Background:= EditColor;
     Edit_Masked:= Masked;
     Edit_MaskChar:= '*';
     Edit_MaxChars:= Width;
     Edit_Text:='';
     inherited Create(Parent,X1,Y1,X2,Y2);
end;

procedure TEditBox.SetText(Text: String);
Var
   OldText:String;

begin
     OldText:=GetText;
     If Length(Text) > Edit_MaxChars then begin
        SetLength(Text,Edit_MaxChars);
     end;
     Edit_Text:= Text;
     If GetText <> OldText then
        Draw;
end;

function TEditBox.GetText: String;
begin
     Result:= Edit_Text;
end;

procedure TEditBox.SetTextColor(Col: Byte);
begin
     Edit_Color:= Col;
end;

function TEditBox.GetTextColor: Byte;
begin
     Result:= Edit_Color;
end;

procedure TEditBox.SetBackgroundColor(Col: Byte);
begin
     Edit_Background:= Col;
end;

function TEditBox.GetBackgroundColor: Byte;
begin
     Result:= Edit_Background;
end;

procedure TEditBox.SetMasked(Masked: Boolean);
begin
     Edit_Masked:= Masked;
     Draw;
end;

function TEditBox.GetMasked: Boolean;
begin
     Result:= Edit_Masked;
end;

procedure TEditBox.SetMaskChar(MChar: Char);
begin
     Edit_MaskChar:= MChar;
end;

function TEditBox.GetMaskChar: Char;
begin
     Result:= Edit_MaskChar;
end;

procedure TEditBox.SetMaxChars(Mx: Integer);
begin
     If Mx > Edit_Width then begin
        Edit_MaxChars:= Edit_Width;
     end else begin
         Edit_MaxChars:= Mx;
     end;
end;

function TEditBox.GetMaxChars: Integer;
begin
     Result:= Edit_MaxChars;
end;

procedure TEditBox.Draw;
Var
   i:integer;

begin
     Screen.LockUpdates;
     For i:=Edit_X to Edit_X+Edit_Width do begin
         Screen.DrawCharXY(GetTag,i,Edit_Y,' ',Edit_Color,Edit_Background);
     end;
     If Edit_Masked then begin
        For i:=1 to Length(Edit_Text) do begin
            Screen.DrawCharXY(GetTag,Edit_X+i-1,Edit_Y,Edit_MaskChar,Edit_Color,Edit_Background);
        end;
     end else begin
         Screen.DrawXY(GetTag,Edit_X,Edit_Y,Edit_Text,Edit_Color,Edit_Background);
     end;
     Screen.UnLockUpdates;
     Screen.SetCursorPosition(Edit_X+Length(Edit_Text),Edit_Y);
end;

procedure TEditBox._OnKeyPress(Key: Char);
Var
   Temp:String;

begin
     If (Ord(Key) > 31) and (Ord(Key) < 127) then begin
        SetText(GetText+Key);
     end else begin
         Case Ord(Key) of
              8:Begin
                  Temp:= GetText;
                  If Length(Temp) > 0 then
                     SetLength(Temp,Length(Temp)-1);
                  SetText(Temp);
              end;
         end;
     end;
     inherited _OnKeyPress(Key);
end;

procedure TEditBox._OnMouseEnter;
begin
     inherited _OnMouseEnter;
end;

procedure TEditBox._OnMouseLeave;
begin
     inherited _OnMouseLeave;
end;

procedure TEditBox._OnClick;
begin
     Screen.SetCursorPosition(Edit_X+Length(Edit_Text),Edit_Y);
     Screen.ShowCursor(True);
     inherited _OnClick;
end;

end.

